package com.aizhixin.lab.project.course.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.company.domain.AccountDomain;
import com.aizhixin.lab.company.service.UserService;
import com.aizhixin.lab.course.classes.dto.CourseClassDTO;
import com.aizhixin.lab.project.course.domain.AddProjectClassDomain;
import com.aizhixin.lab.project.course.domain.QueryProjectClassDomain;
import com.aizhixin.lab.project.course.domain.StuCreationGroupDomain;
import com.aizhixin.lab.project.course.domain.StuRandomDomain;
import com.aizhixin.lab.project.course.entity.ProjectClass;
import com.aizhixin.lab.project.course.entity.ProjectClassStu;
import com.aizhixin.lab.project.course.entity.ProjectGroup;
import com.aizhixin.lab.project.course.entity.TeacherProjectModule;
import com.aizhixin.lab.project.course.repository.ProjectClassRepository;
import com.aizhixin.lab.project.course.repository.ProjectClassStuRepository;
import com.aizhixin.lab.project.course.service.ProjectClassService;
import com.aizhixin.lab.project.course.service.ProjectClassStuService;
import com.aizhixin.lab.project.course.vo.ProjectClassVo;
import com.aizhixin.lab.project.template.domain.ProjectModuleDomain;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping("/api/v1/teacher/project/class")
@RestController
@Api(value = "班级管理模块", tags = "班级管理")
public class ProjectClassController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private ProjectClassService classService;
    @Autowired
    private ProjectClassStuService stuService;


    @RequestMapping(value = "/addProjectClass", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加班级或学生", httpMethod = "POST", notes = "添加班级或学生<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token,
                                  @RequestBody List<AddProjectClassDomain> domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }try{
            result = classService.addProjectClass(domain, account);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }

    @GetMapping(value = "/addPracticeProjectClass")
    @ApiOperation(value = "添加班级或学生-联系项目-备选项目", httpMethod = "POST", notes = "添加班级或学生-联系项目-备选项目<br><br><b>lwq</b>")
    public ResponseEntity<?> addPracticeProjectClass(@RequestHeader("Authorization") String token,
                                 @RequestParam String projectId,Long teaId,String parentId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }try{
            result = classService.addPracticeProjectClass(projectId,teaId,parentId, account);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/like", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "点赞", httpMethod = "GET", notes = "点赞<br><br><b>lwq</b>")
    public ResponseEntity<?> like(@RequestHeader("Authorization") String token, @RequestParam String projectClassId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }try{
            result = stuService.likeOrUrge(projectClassId,10);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/urge", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "督促", httpMethod = "GET", notes = "督促<br><br><b>lwq</b>")
    public ResponseEntity<?> urge(@RequestHeader("Authorization") String token, @RequestParam String projectClassId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }try{
            result = stuService.likeOrUrge(projectClassId,20);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/stuUrge", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "学生端督促", httpMethod = "GET", notes = "学生端督促<br><br><b>lwq</b>")
    public ResponseEntity<?> stuUrge(@RequestHeader("Authorization") String token, @RequestParam String projectId, @RequestParam Long stuId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }try{
            result = stuService.urge(projectId,stuId);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findProjectClass", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "按项目id查询改项目下的班级", response = Void.class, notes = "按项目id查询改项目下的班级<br>@author lwq")
    public ResponseEntity<?> findCourseClass(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "projectId项目Id", required = false) @RequestParam(value = "projectId", required = false) String projectId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        List<ProjectClassVo> projectClass = classService.findProjectClass(projectId);
        result.put(ApiReturnConstants.DATA, projectClass);
        result.put(ApiReturnConstants.SUCCESS, true);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findProjectClassStu", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "查询项目班级下的学生列表", response = Void.class, notes = "查询项目班级下的学生列表<br>@author lwq")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = AccountDomain.class),})
    public ResponseEntity<?> findProjectClassStu(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "QueryProjectClassDomain", required = false) @RequestBody QueryProjectClassDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        result= classService.getStuPage(domain);
//        result.put(ApiReturnConstants.DATA, result);
//        result.put(ApiReturnConstants.SUCCESS, true);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/deleteClassStu", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "DELETE", value = "删除项目下的学生", response = Void.class, notes = "删除项目下的学生<br>@author lwq")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = AccountDomain.class),})
    public ResponseEntity<?> deleteClassStu(@RequestHeader("Authorization") String token,
                                                 @RequestParam List<String> ids) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        result= stuService.deleteStu(ids);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/deleteClass", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "DELETE", value = "删除项目下的学生", response = Void.class, notes = "删除项目下的学生<br>@author lwq")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = AccountDomain.class),})
    public ResponseEntity<?> deleteClass(@RequestHeader("Authorization") String token,
                                            @RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result =  new HashMap<String, Object>();
        result= stuService.deleteClass(id);
        return new ResponseEntity(result, HttpStatus.OK);
    }






}
